<!--
 * @title:
 * @Description: https://lbs.qq.com/service/webService/webServiceGuide/webServiceOverview
 * @Author: JZT.饶慧勇
 * @Date: 2025-05-29 10:18:49
 * @LastEditors: JZT.饶慧勇
 * @LastEditTime: 2025-05-30 13:42:03
-->
<script lang="ts" setup>
defineOptions({
  name: 'MapPage'
});

// let mapContext: any = null;

const latitude = ref(30.505762);
const longitude = ref(114.401755);

const coors = [
  30.505593, 114.400525, 30.505724, 114.40060700000001, 30.505724, 114.40060700000001, 30.505754, 114.40069000000001,
  30.505754, 114.40069000000001, 30.505788, 114.400833, 30.505788, 114.400833, 30.505972999999997, 114.40087100000001,
  30.505972999999997, 114.40087100000001, 30.505983999999998, 114.400974, 30.505983999999998, 114.400974,
  30.506030999999997, 114.40139900000001, 30.506030999999997, 114.40139900000001, 30.506117999999997, 114.40205500000002
];
// for (let i = 2; i < coors.length; i++) {
//   coors[i] = coors[i - 2] + coors[i] / 1000000;
// }
// 转换函数
function convertCoordinates<T>(rawArray: T[]) {
  const result: { latitude: T; longitude: T }[] = [];

  // 确保数组长度是偶数
  if (rawArray.length % 2 !== 0) {
    console.warn('坐标数据不完整，最后一个坐标将被忽略');
    rawArray = rawArray.slice(0, -1);
  }

  // 每两个元素为一组，转换为一个坐标对象
  for (let i = 0; i < rawArray.length; i += 2) {
    result.push({
      latitude: rawArray[i],
      longitude: rawArray[i + 1]
    });
  }

  return result;
}
const points = convertCoordinates(coors);
const polyline = ref([
  {
    points
  }
]);

const markers = ref<any>([
  {
    id: 0,
    latitude: points[0].latitude,
    longitude: points[0].longitude,
    title: '起点',
    label: {
      content: '起点'
    },
    iconPath: '/static/logo.png',
    width: 24,
    height: 24
  },
  {
    id: 1,
    latitude: points[points.length - 1].latitude,
    longitude: points[points.length - 1].longitude,
    title: '终点',
    label: {
      content: '终点'
    },
    iconPath: '/static/logo.png',
    width: 24,
    height: 24
  }
]);
// const indoorInfo = ref();

// function initIndoorMap() {
// console.log(mapContext.getIndoorFloor());
// mapContext
//   .getIndoorFloor()
//   .then(result => {
//     console.error('加载室内地图:', result);
//   })
//   .catch(err => {
//     console.error('加载室内地图失败:', err);
//   });
// mapContext.getIndoorFloor({
//   success: res => {
//     if (res.indoorData && res.indoorData.building) {
//       indoorInfo.value = res.indoorData.building;
//       console.log(indoorInfo.value);
//     } else {
//       uni.showToast({
//         title: '未找到室内地图数据',
//         icon: 'none'
//       });
//     }
//   },
//   fail: err => {
//     console.error('加载室内地图失败:', err);
//   }
// });
// }

onReady(() => {
  // mapContext = uni.createMapContext('worldCityMap');
  // mapContext.addMarkers()
  // 延迟确保地图加载完成
  // setTimeout(() => {
  //   console.log('mapContext', mapContext);
  //   mapContext.openMapApp({
  //     longitude: longitude.value,
  //     latitude: latitude.value,
  //     destination: '光谷广场',
  //     fail() {
  //       uni.showToast({
  //         title: '地图拉起失败'
  //       });
  //     }
  //   });
  //   // initIndoorMap();
  // }, 1000);
});

onUnload(() => {
  // mapContext = null;
});
</script>

<template>
  <map
    id="worldCityMap"
    style="width: 100vh; height: 500px"
    :latitude="latitude"
    :longitude="longitude"
    :show-compass="true"
    :enable-indoorMap="true"
    :show-location="true"
    :polyline="polyline"
    :markers="markers"
  />
</template>

<style lang="scss" scoped></style>
